<template>
  <el-dialog :visible="showDialog" :title="formDate.id?'编辑经纪':'添加经纪'" @close="closeFn">
    <el-form ref="form" :model="formDate" label-width="100px" :rules="rules">
      <el-form-item prop="name" label="名称">
        <el-input v-model="formDate.name" />
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="formDate.email" />
      </el-form-item>
      <el-form-item prop="phone" label="电话">
        <el-input v-model="formDate.phone" />
      </el-form-item>
      <el-form-item prop="address" label="地址">
        <el-input v-model="formDate.address" />
      </el-form-item>
      <el-form-item prop="desc" label="描述">
        <el-input v-model="formDate.desc" type="textarea" />
      </el-form-item>
      <el-form-item prop="image" label="封面">
        <uploadImage ref="uploadImage" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="confirmFn">确定</el-button>
          <el-button size="small" @click="closeFn">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addAgents, getAgentsById, editAgents } from '@/api/agents'
import uploadImage from '@/components/uploadImage'
export default {
  components: {
    uploadImage
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formDate: {
        name: '', // 经纪名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        address: '', // 地址
        desc: '', // 描述
        image: '' // 封面图
      },
      rules: {
        name: [{ required: true, message: '请输入经纪名称', trigger: 'blur' }],
        email: [{ required: true, message: '请输入经纪邮箱', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入经纪电话', trigger: 'blur' }],
        address: [{ required: true, message: '请输入经纪地址', trigger: 'blur' }],
        desc: [{ required: true, message: '请输入经纪描述', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 照片回显
    if (this.formDate.image) {
      this.$refs.uploadImg.fileList.push({
        url: this.formDate.image
      })
    }
  },
  methods: {
    // 新增 编辑
    async confirmFn() {
      // 校验成功
      await this.$refs.form.validate()
      this.$nextTick(() => {
        this.formDate.image = this.$refs.uploadImage.fileList[0].url
      })
      if (this.formDate.id) {
        // 编辑
        await editAgents(this.formDate)
      } else {
        // 新增
        await addAgents(this.formDate)
      }
      // 关闭弹窗
      this.closeFn()
      // 刷新列表
      this.$emit('updateEvent')
    },
    // 关闭
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formDate = {
        name: '', // 经纪名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        address: '', // 地址
        desc: '', // 描述
        image: '' // 封面图
      }
      // 清空校验
      this.$refs.form.resetFields()
      // 清空图片数据
      this.$nextTick(() => {
        this.$refs.uploadImage.fileList = []
      })
    },
    // 编辑回显
    async editContent(id) {
      const res = await getAgentsById(id)
      this.formDate = res.data
      this.$nextTick(() => {
        this.$refs.uploadImage.fileList = [{ url: this.formDate.image }]
      })
    }
  }
}
</script>

<style>
</style>
